package com.lanju.app.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.lanju.app.message.bean.B108Request;
import com.lanju.app.message.bean.B108Response;
import com.lanju.app.message.bean.B108Response.CateBean;
import com.lanju.app.util.AppUtil;
import com.lanju.entity.User;

/**
 * 按目录获取商品
 * @author fly
 *
 */
@Service("B108Service")
public class B108Service extends BaseService<B108Request,B108Response>{

	@Override
	public B108Response doService(B108Request request, User u) throws Exception {
		B108Response res = new B108Response();		
		List<Object> param = new ArrayList<>();
		StringBuilder sql = new StringBuilder(" SELECT c.id cateId, c.`name` name, b.id prodId, b.`name` prodName, b.path FROM ( ");
		sql.append(" SELECT x.prod_id,x.cate_id FROM product_sku x,(SELECT cate_id,MAX(created_time) created_time FROM product_sku GROUP BY cate_id)  y ");
		sql.append(" WHERE x.cate_id = y.cate_id AND x.created_time = y.created_time ORDER by x.cate_id  ");
		sql.append(" )a LEFT JOIN product b ON a.prod_id=b.id LEFT JOIN product_cate c ON a.cate_id=c.id ");
		List<CateBean> list = dao.list(sql.toString(), CateBean.class,  param.toArray());		
		for(CateBean bean: list){
			bean.setPath(StringUtils.isBlank(bean.getPath())?"":(AppUtil.oss_psth+bean.getPath()));
		}
		res.setCates(list);
		res.setSuccess(true);
		return res;
	}
}
